import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import {config} from '../../shared/smartadmin.config';
import { Router,ActivatedRoute} from "@angular/router"; //路由引入
declare var $: any;
declare var echarts: any;
@Component({
    selector: 'sa-order',
    templateUrl: './device-offline.component.html',
    styleUrls:['./device-offline.component.css']
})
export class DeviceOfflineComponent implements OnInit {
	tableData:any[] = []; //table数据
	resultTableData:any[] = [] //客户端分页数据暂存
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	companyCarModule:any = {
		selectedCompanyName:'',//过滤条件:公司name
		selectedCompanyId:'', //过滤条件:公司Id
		useTypeL1:'',//过滤条件:车辆类型一类
		useTypeL2:'',//过滤条件:车辆类型二类
		carNumber:'', //车牌号码
		deviceId:'',//设备ID
	}
	offline:any = {
		offlineTime:'3600',//离线时长过滤条件
		startTime:'',//开始时间
		endTime:'',//结束时间
		searchKey:'', //搜索keyword
		weightType:'0',
		sortType:'1',
		dateType:'0',
		sortTime:'2',
	}
	gps:any = {
		offlineTime:'600',//离线时长过滤条件
		startTime:'',//开始时间
		endTime:'',//结束时间
		searchKey:'', //搜索keyword
		sortType:'',
		dateType:'1',
		sortTime:'2',
	}
	TabType:string = 'deviceOffline';//tab激活标识,默认设备离线页面
	constructor(private jsonApiService: JsonApiService,private router: Router) {}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time){
		this.offline.startTime = time.startTime;
		this.offline.endTime = time.endTime;
	}
	// 接收子组件传递过来的数据 
	getSelectTableTime2(time){
		this.gps.startTime = time.startTime;
		this.gps.endTime = time.endTime;
	}
	ngOnInit() {
		this.load();
	}
	//Tab 切换
	tabChange(type){
		this.TabType = type;
		this.curPage = 1;
		this.pageSize = 10;
		this.load();
	}
	//初始化加载list数据
	load() {
		let data:any,Url:string;
		if(this.TabType == 'deviceOffline'){
			if(!this.companyCarModule.selectedCompanyId){
				this.jsonApiService.message('请选择公司');
				return;
			}
			let timeCheck=common.correctTimeParams(this.offline.startTime,this.offline.endTime,0);
			if(!timeCheck.status){
				this.jsonApiService.message(timeCheck.message);
				return;
			}
			Url = 'realtime/offlineCarPage.html';
			data = {
				pageSize:this.pageSize,
				nowPage:this.curPage,
				parameters:{
					searchKey:this.offline.searchKey.trim(),
					companyId:this.companyCarModule.selectedCompanyId,
					deviceId:this.companyCarModule.deviceId,
					offlineTime:this.offline.offlineTime,
					startTime:this.offline.startTime,
					endTime:this.offline.endTime,
					weightType:this.offline.weightType,
					useTypeL2:this.companyCarModule.useTypeL2,
					dateType:this.offline.dateType,
					sortType:this.offline.sortType,
					sortTime:this.offline.sortTime
				}
			}
		}else if(this.TabType == 'gpsError'){
			if(!this.companyCarModule.selectedCompanyId){
				this.jsonApiService.message('请选择公司');
				return;
			}
			let timeCheck=common.correctTimeParams(this.gps.startTime,this.gps.endTime,0);
			if(!timeCheck.status){
				this.jsonApiService.message(timeCheck.message);
				return;
			}
			Url = 'realtime/gpsException.html';
			data = {
				pageSize:this.pageSize,
				nowPage:this.curPage,
				parameters:{
					searchKey:this.offline.searchKey.trim(),
					companyId:this.companyCarModule.selectedCompanyId,
					deviceId:this.companyCarModule.deviceId,
					offlineTime:this.gps.offlineTime,
					startTime:this.gps.startTime,
					endTime:this.gps.endTime,
					useTypeL2:this.companyCarModule.useTypeL2,
					sortType:this.gps.sortType,
					dateType:this.gps.dateType,
					sortTime:this.gps.sortTime
				}
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http(Url, reqdata).subscribe(result => {
			if(!result.success){
				this.jsonApiService.message(result.message);
				return;
			}
			for(let item of result.data){
				item.offlineSecond = common.secondToDate(item.offlineSecond);
			}
			this.tableData = result.data;
			this.totalCount = result.data.length;
			this.client_pages(0,this.pageSize);
		})
	}
	//导出
	dataExceport(){
		if(!this.companyCarModule.selectedCompanyId){
			this.jsonApiService.message('请选择公司');
			return;
		}
		var parameters:any,Url:string;
		if(this.TabType == 'deviceOffline'){
			Url = 'realtime/exportOfflineData.html';
			parameters = {
				searchKey:this.offline.searchKey,
				companyId:this.companyCarModule.selectedCompanyId,
				deviceId:this.companyCarModule.deviceId,
				offlineTime:this.offline.offlineTime,
				startTime:this.offline.startTime,
				endTime:this.offline.endTime,
				weightType:this.offline.weightType,
				useTypeL2:this.companyCarModule.useTypeL2,
				dateType:this.offline.dateType,
				sortType:this.offline.sortType,
				sortTime:this.offline.sortTime,
			}
		}else if(this.TabType == 'gpsError'){
			Url = 'realtime/exportGpsExceptionData.html';
			parameters = {
				searchKey:this.offline.searchKey,
				companyId:this.companyCarModule.selectedCompanyId,
				deviceId:this.companyCarModule.deviceId,
				offlineTime:this.gps.offlineTime,
				startTime:this.gps.startTime,
				endTime:this.gps.endTime,
				useTypeL2:this.companyCarModule.useTypeL2,
				sortType:this.gps.sortType,
				dateType:this.gps.dateType,
				sortTime:this.gps.sortTime
			}
		}
		// let timeCheck = common.correctTime(parameters, ['sortType', 'sortTime', 'dateType'])
		// if(!timeCheck.status){
		// 	this.jsonApiService.message(timeCheck.message);
		// 	return;
		// }
		common.exprotDataFile(parameters, 'param', config.BaseURL + Url)
	}
	ngOnDestroy(){
		document.onclick = null;
	}
	//刷新
    refresh(){
    	this.load();
    }
	//查询
	table_search(){
		this.curPage=1;
		this.load();
	}
	//前端分页
	client_pages(start,length){
		this.resultTableData = this.tableData.slice(start,start+length);
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.client_pages(e.first,e.rows);
	}
	//公司选中回调[设备离线]
	companySelected(data){
		this.companyCarModule.selectedCompanyId = data.companyId;
		this.companyCarModule.selectedCompanyName = data.companyName;
		this.companyCarModule.deviceId = data.deviceId;
		this.companyCarModule.carNumber = data.carNumber;
		this.companyCarModule.useTypeL2 = data.useTypeL2;
		this.companyCarModule.useTypeL1 = data.useTypeL1;
	}
}
